package com.cloudcode.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloudcode.common.api.ApiResult;
import com.cloudcode.document.DocumentContent;
import com.cloudcode.dto.req.DocumentContentReq;
import com.cloudcode.dto.resp.DocumentContentResp;
import com.cloudcode.service.SearchCoreService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author yunst
 * @data 2023/10/20 18:44
 */
@RequestMapping("/search")
@RestController
public class SearchCoreController {
    @Resource
    private SearchCoreService searchCoreService;

    /**
     * 搜索
     */
    @RequestMapping()
    public ApiResult<Page<DocumentContentResp>> query(@RequestParam("keyword") String keyword, @RequestParam(value = "lastId", required = false) String lastId, @RequestParam(value = "size", defaultValue = "3") Integer size) {
        return ApiResult.success(searchCoreService.search(keyword, lastId, size));
    }

    /**
     * 搜索预览
     */
    @RequestMapping("/preview/list")
    public ApiResult<List<DocumentContentResp>> previewList(@RequestParam("keyword") String keyword, @RequestParam(value = "size", defaultValue = "2") Integer size) {
        return ApiResult.success(searchCoreService.previewList(keyword, size));
    }

    /**
     * 删除
     */
    @DeleteMapping()
    public ApiResult delete(@RequestParam("id") String id) {
        searchCoreService.delete(id);
        return ApiResult.ok();
    }


    /**
     * 保存
     */
    @PostMapping()
    public ApiResult saveDocument(@RequestBody DocumentContentReq documentContentReq) {
        searchCoreService.saveDocument(documentContentReq);
        return ApiResult.ok();
    }

}
